package bb.lanxing.lib.devices.base.converter;

import android.bluetooth.BluetoothGattCharacteristic;
import bb.lanxing.lib.devices.base.CharacteristicConverter;
import bb.lanxing.lib.devices.core.ble.IGattClient;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import android.util.Log;
import java.math.BigDecimal;
import java.util.UUID;

public class IndoorbikeConverter implements CharacteristicConverter {
    final static private String TAG = "IndoorbikeConverter";
    final static private boolean DebugFTMS = false;

    public void onIndoorbike(int i, double i2, int i3, int i4, int i5, int i6,double i7, int i8) {
    }

    public int unsignedByteToInt(byte b) {
        return b & 0xFF;
    }

    public int unsignedBytesToInt24(byte b0, byte b1, byte b2) {
        return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
                + (unsignedByteToInt(b2) << 16) ;
    }
    @Override
    public final void convert(IGattClient iGattClient, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        int intValueFlag1byte =0;
        int intValueFlag2byte = 0;
        double doubleValueSpeed = 0;
        int intValueCadence = 0;
        int intValuePower = 0;
        int intValueResistanceLevel = 0;
        int intValueEnergy = 0;
        double doubleValueTotalDistance = 0;
        int intValueHeartRate = 0 ;

        if (bluetoothGattCharacteristic.getUuid().equals(UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_INDOOR_BIKE_DATA))) {
            intValueFlag1byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
            intValueFlag2byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
            if(DebugFTMS) {
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)*100)/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueCadence = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/2;
                intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                doubleValueTotalDistance = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))*1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
            }else if((intValueFlag1byte == 116) && (intValueFlag2byte == 11)) {  //0x74 0x0B Chongqing LD indoor bike,  FS-LD CX50
                doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2))/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueCadence = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 4)/2);
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 9);
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 13);
                byte[] value = bluetoothGattCharacteristic.getValue();
                doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[6],value[7],value[8]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 18);
            } else if ((intValueFlag1byte == 118) && (intValueFlag2byte == 11))  { //0x76 0x0B Decathlon indoor bike, Domyos-Bike-XXXX
                doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2))/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueCadence = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 6)/2);
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 13);
                intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 15);
                byte[] value = bluetoothGattCharacteristic.getValue();
                doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[8],value[9],value[10]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 20);
            }
            onIndoorbike(iGattClient.getType(), doubleValueSpeed,intValueCadence,intValuePower,intValueResistanceLevel,intValueEnergy,doubleValueTotalDistance,intValueHeartRate);
        }
    }
}
